using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

/// <summary>
/// 点击物体后，根据物体下挂载节点，自动生成组合效果
/// </summary>
[CustomEditor(typeof(WindowRoot), true)]
public class WindowRootInspector : Editor
{
    private List<GameObject> mCompList = new List<GameObject>();

    private void OnEnable()
    {
        mCompList.Clear();
        WindowRoot root = (WindowRoot)target;
        if (root != null && root.WindowLayer == WindowLayer.BaseUI && root.EnableCompShow)
        {
            Transform[] childs = root.GetComponentsInChildren<Transform>();
            foreach (Transform child in childs)
            {
                if (child.childCount == 0 && child.name.Contains("Comp"))
                {
                    string path = $"Assets/Resources/{PathDefine.CompPath}{child.name}.prefab";
                    GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                    GameObject go = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
                    go.transform.SetParent(child);
                    go.transform.localPosition = Vector3.zero;
                    go.transform.localEulerAngles = Vector3.zero;
                    mCompList.Add(go);
                }
                
            }
        }
       
    }

    private void OnDisable()
    {
        foreach (GameObject comp in mCompList)
        {
            DestroyImmediate(comp);
        }

        mCompList.Clear();
    }
}